package com.fivestyle.controller;

import com.fivestyle.common.lang.Result;
import com.fivestyle.entity.Book;
import com.fivestyle.entity.Orders;
import com.fivestyle.service.BookService;
import com.fivestyle.service.OrderService;
import com.fivestyle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundKeyOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主页数据请求
 * @author yangjing
 * @since 2021-8-8
 */
@RestController
@RequestMapping("/graphical")
public class StatisticsController {

    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;
    @Autowired
    private OrderService orderService;

    @GetMapping("/")
    public Result graphical(){

        int userList = userService.list().size();
        int bookslist = bookService.list().size();
        List<Orders> ordersList = orderService.list();
        int order = ordersList.size();

        BigDecimal sum = new BigDecimal("0");
        for (Orders orders : ordersList) {
            BigDecimal moneyTotal = orders.getMoneyTotal();
            sum=sum.add(moneyTotal);
        }
        HashMap<String,Object> map = new HashMap<>();

        map.put("user",userList);
        map.put("book",bookslist);
        map.put("order",order);
        map.put("money",sum);
        return Result.succ(map);
    }

    @GetMapping("/pie")
    public Result pie(){
        ArrayList<HashMap<String,Object> > list=bookService.pie();

        return Result.succ(list);

    }

}
